package com.hz.house.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hz.house.service.SellManService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hz.house.service.WxgzhSellHouseService;

/**
 * <p>卖房好帮手公众号对接接口
 * @author lfw
 *
 */
@Slf4j
@RestController
@RequestMapping("/wxgzh/sell/msg")
public class WxgzhSellHouseController {

	@Autowired
	private WxgzhSellHouseService wxgzhSellHouseService;

	@Autowired
	private SellManService sellManService;
	
	/**
	 * <p> 微信推送消息的回调接口
	 * @returnu
	 */
	@RequestMapping("/wxcallback")
	public String sellGzhCallBack(HttpServletRequest req,HttpServletResponse rep) {
		
		String method = req.getMethod();
		if(method.equals("GET"))
			return this.wxgzhSellHouseService.validateToken(req);
		//post就是其他业务数据回调了
		else
			this.wxgzhSellHouseService.sellHouseWxgzhCallBack(req,rep);
		return "success";
	}
	
	
	/**
	 * 微信公众号网页授权
	 * @param req
	 * @param rep
	 */
	@RequestMapping("/bindUserInfo")
	public void bindUserUnionId(HttpServletRequest req,HttpServletResponse rep) {
		  int res = this.wxgzhSellHouseService.bindUserUnionId(req);
	        //返回值判定再跳转到哪个
	        String page = "http://www.fangjiadianpingbang.com/gzh/";
	        switch(res) {
	            case 0:
	                //错误
	                page+="error.html";
	                break;
	            case 1:
	                //登录重试
	                page+="retry.html";
	                break;
	            case 2:
	                //成功
	                page+="success.html";
	                break;
	        }
	        try {
	            //直接跳转到结束提示页
	            rep.sendRedirect(page);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	}
}
